package com.ruoyi.common.enums.business;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum ApplicableScopeEnum {

    UNIVERSAL(1, "全场通用"),
    SPECIFIED_PRODUCT(2, "指定商品"),
    SPECIFIED_CATEGORY(3, "指定分类");

    private final Integer code;
    private final String desc;

    private static final Map<Integer, ApplicableScopeEnum> ENUM_MAP;

    static {
        ApplicableScopeEnum[] values = values();
        Map<Integer, ApplicableScopeEnum> map = new HashMap<>(values.length);
        for (ApplicableScopeEnum value : values) {
            map.put(value.code, value);
        }
        ENUM_MAP = Collections.unmodifiableMap(map);
    }

    /**
     * 根据code查找枚举
     */
    public static ApplicableScopeEnum getEnumByCode(Integer code) {
        ApplicableScopeEnum applicableScopeEnum = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(applicableScopeEnum)) {
            throw new RuntimeException("无效的适用范围编码: " + code);
        }
        return applicableScopeEnum;
    }

    /**
     * 获取描述
     */
    public static String getDescByCode(Integer code) {
        ApplicableScopeEnum applicableScopeEnum = ENUM_MAP.get(code);
        return ObjectUtil.isEmpty(applicableScopeEnum) ? "未知范围" : applicableScopeEnum.getDesc();
    }

    /**
     * 判断是否为全场通用
     */
    public static boolean isUniversal(Integer code) {
        return UNIVERSAL.getCode().equals(code);
    }

}
